package com.haochang.chunkvod.controller.adapter.cash;

import java.util.List;

import com.haochang.base.BaseEditText;
import com.haochang.base.EventProxy;
import com.haochang.base.IEventConstant;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.activity.cash.NonOperatingGoodsDetailsActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.cash.GoodInfo;
import com.haochang.chunkvod.model.cash.NonOperateEnum;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

/**
 * 营业外商品详情适配器
 */
public class NonOperatingRevenueAdapter extends QuickAdapter implements KeyBoardDialog.KeyBoardOnSureListener
{
	private Context mContext;
	private BaseEditText onClickTextView;
	public NonOperateEnum activityEnum = NonOperateEnum.NON_OPERATE;

	public NonOperatingRevenueAdapter(Context context, int layoutResId, List data)
	{
		super(context, layoutResId, data);
		mContext = context;
	}

	@Override
	protected void convert(final BaseAdapterHelper helper, final Object item)
	{
		final GoodInfo goodsInfo = (GoodInfo) item;
		helper.setText(R.id.tv_goodsName, goodsInfo.getGoodsName());
		helper.setText(R.id.et_num, goodsInfo.getOrderNum() + "");
		helper.getView(R.id.img_plus).setTag(goodsInfo);
		helper.getView(R.id.img_plus).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.img_minus).setTag(goodsInfo);
		helper.getView(R.id.img_minus).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.et_num).setTag(helper.getPosition());
		BaseEditText et_num = helper.getView(R.id.et_num);
		et_num.addTextChangedListener(textWatcher);
		helper.getView(R.id.et_num).setTag(R.id.non_operate_revenue_num, helper.getView(R.id.et_num));
		helper.getView(R.id.et_num).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.tv_goodsName).setTag(goodsInfo);
		helper.getView(R.id.tv_goodsName).setOnClickListener(onBaseClickListener);
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{

			switch (v.getId())
			{
			case R.id.tv_goodsName:
				GoodInfo goodsInfo = (GoodInfo) v.getTag();
				Intent intent = new Intent(mContext, NonOperatingGoodsDetailsActivity.class);
				intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, activityEnum == NonOperateEnum.NON_OPERATE ? "营业外商品详情" : "赔偿详情");
				intent.putExtra(IntentKey.MATERIELCODEVALUE, goodsInfo.getMateriel_code());
				intent.putExtra(IntentKey.GOODSNAMETITLE, activityEnum == NonOperateEnum.NON_OPERATE ? "商品名：" : "赔偿物件名：");
				intent.putExtra(IntentKey.GOODSNAMEVALUE, goodsInfo.getGoodsName());
				intent.putExtra(IntentKey.PRICEVALUE, SaleUtils.getSaleString(goodsInfo.getPrice()));
				mContext.startActivity(intent);
				break;
			case R.id.et_num:
				onClickTextView = (BaseEditText) v.getTag(R.id.non_operate_revenue_num);
				KeyBoardDialog keyBoardDialog = new KeyBoardDialog(mContext);
				keyBoardDialog.setKeyBoardOnSureListener(NonOperatingRevenueAdapter.this);
				keyBoardDialog.show();
				break;
			case R.id.img_minus:
				GoodInfo goodsInfo1 = (GoodInfo) v.getTag();
				if (goodsInfo1.getOrderNum() > 0)
				{
					goodsInfo1.setOrderNum(goodsInfo1.getOrderNum() - 1);
					notifyDataSetChanged();
				} else
					ToastUtils.showText(mContext, "数量不能小于0");
				break;
			case R.id.img_plus:
				GoodInfo goodsInfo2 = (GoodInfo) v.getTag();
				goodsInfo2.setOrderNum(goodsInfo2.getOrderNum() + 1);
				notifyDataSetChanged();
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void setValue(String value)
	{
		if (value.contains("."))
			ToastUtils.showText(mContext, R.string.error_number);
		else if (value.length() > 9)
			ToastUtils.showText(mContext, R.string.error_max_number);
		else
		{
			int position = (Integer) onClickTextView.getTag();
			GoodInfo goodInfo = (GoodInfo) data.get(position);
			goodInfo.setOrderNum(Integer.parseInt(value));
			onClickTextView.setText(value);
		}
	}

	private TextWatcher textWatcher = new TextWatcher()
	{
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after)
		{

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count)
		{

		}

		@Override
		public void afterTextChanged(Editable s)
		{
			EventProxy.notifyEvent(IEventConstant.NON_OPERNATE_GOODS);
		}
	};

}
